package com.jxs.android.weixi;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jxs.android.weixi.data.AchvInfo;
import com.telecom.weixi.R;

public class MyAchiverment extends MyCommnList {

	private List<AchvInfo> resInfos = new ArrayList<AchvInfo>();
	private AchvInfoAdapter adapter;

	@Override
	public String getMethod() {
		return "getPerformanc";
	}

	@Override
	public Object paserData(SoapObject retMsgObject) {
		resInfos.clear();
		/**
		 * @id 0
		 * @serv_gz_income:我的收入-公众
		 * @serv_zq_income:我的收入-政企
		 * @serv_all_income:我的收入-总收入
		 */
		AchvInfo wrkInfo = new AchvInfo();
		wrkInfo.id = 0;
		wrkInfo.title = "我的收入";
		wrkInfo.name_1 = "公众";
		wrkInfo.name_1_value = retMsgObject.getPropertySafelyAsString("serv_gz_income");
		wrkInfo.name_2 = "政企";
		wrkInfo.name_2_value = retMsgObject.getPropertySafelyAsString("serv_zq_income");
		wrkInfo.name_3_value = retMsgObject.getPropertySafelyAsString("serv_all_income");
		resInfos.add(wrkInfo);

		/**
		 * @id 1
		 * @owe_num:累计欠费-用户数
		 * @owe_fee:累计欠费
		 */
		wrkInfo = new AchvInfo();
		wrkInfo.id = 1;
		wrkInfo.title = "累计欠费";
		wrkInfo.name_1 = "用户数";
		wrkInfo.name_1_value = retMsgObject.getPropertySafelyAsString("owe_num");
		wrkInfo.name_3_value = retMsgObject.getPropertySafelyAsString("owe_fee");
		resInfos.add(wrkInfo);
		return resInfos;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateList(Object msgObj) {
		adapter.setAchvInfos((List<AchvInfo>) msgObj);
		adapter.notifyDataSetChanged();
	}

	@Override
	public OnItemClickListener getItemClickListener() {
		return new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int location, long arg3) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				String title = "";
				if (location - 1 == 0) {
					title = "当月收入";
					intent.setClass(MyAchiverment.this, MyAchiverment_Income.class);
				} else if (location - 1 == 1) {
					title = "累计欠费";
					intent.setClass(MyAchiverment.this, TableList.class);
				}
				bundle.putString("title", title);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		};
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new AchvInfoAdapter(context);
		return adapter;
	}

	@Override
	public String getTopBarTitle() {
		return getString(R.string.home_succ);
	}

	class AchvInfoAdapter extends BaseAdapter {

		List<AchvInfo> wrkInfos = new ArrayList<AchvInfo>();

		private LayoutInflater inflater = null;
		private ViewHolder holder = null;

		public AchvInfoAdapter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void setAchvInfos(List<AchvInfo> custInfos) {
			this.wrkInfos.clear();
			this.wrkInfos.addAll(custInfos);
		}

		@Override
		public int getCount() {
			return wrkInfos.size();
		}

		@Override
		public AchvInfo getItem(int position) {
			return wrkInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			AchvInfo custInfo = wrkInfos.get(position);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bu_list_item, null);
				holder = new ViewHolder();
				holder.icon_iv = (ImageView) convertView.findViewById(R.id.item_icon);
				holder.title_tv = (TextView) convertView.findViewById(R.id.item_title);
				holder.value_tv = (TextView) convertView.findViewById(R.id.item_des);
				holder.item_right = (TextView) convertView.findViewById(R.id.item_right);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			/*
			 * serv_gz_num:公众类型用户数 serv_zq_num:政企类型用户数 serv_all_num:用户数
			 * cust_gz_num:公众类型客户数 cust_zq_num:政企类型客户数 cust_all_num:客户数
			 */
			holder.title_tv.setText(custInfo.title);
			holder.title_tv.setTextColor(Color.rgb(0, 0, 0));
			if (custInfo.id == 0) {
				holder.value_tv.setText(custInfo.name_1 + ":" + custInfo.name_1_value + custInfo.name_2 + ":" + custInfo.name_2_value);
			} else if (custInfo.id == 1) {
				holder.value_tv.setText(custInfo.name_1 + ":" + custInfo.name_1_value);
			}
			holder.item_right.setText(custInfo.name_3_value);
			return convertView;
		}

		class ViewHolder {
			ImageView icon_iv;
			TextView title_tv;
			TextView value_tv;
			TextView item_right;
		}

	}

}
